$.Class("Invest.Grid",
// static properties
{     
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/invest/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    }
                },
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    }
                },
                requestStart: function(e) {
                    var data = {
                        domain : $("#_domain").val(),
                        status : $("#_status").val(),
                        isFinished : $("#_isFinished").val(),
                        isAjax : true
                    };
                    
                    Invest.Grid.addParams(e, data);
                }
            });

            return dataSource;
        },
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        //height: 500,
	        filterable: false,
	        sortable: true,
	        pageable: true,
	        columns: [{
                field:"id",
                title:"ID",
                width: 30,
                sortable: false,
            },{
                field:"grid_index",
                title:" ",
                width: 40,
                sortable: false,
            },{
                field:"domain",
                title:"Domain",
                sortable: false,
                width: 200
            },{
                field:"status",
                title:"Status",
                sortable: false,
                width: 75
            },{
                field:"profitDaily",
                title:"Profit",
                width: 80,
                template: '${profitDaily}%'
            },{
                field:"duration",
                title:"Duration",
                width: 110,
                template: '${duration} days'
            },{
                field:"planWithdraw",
                title:"Plan Withdraw",
                width: 120,
                template: '$${planWithdraw}'
            },{
                field:"deposit",
                title:"Deposit",
                width: 70,
                template: '$${deposit}'
            },{
                field:"withdraw",
                title:"Withdraw",
                width: 100,
                template: '$${withdraw}'
            },{
                field:"investedDate",
                title:"Invested date",
                width: 160
            },{
            	title:"",
                field:"",         
                width: 100,
                template: '<a href="" onclick="return Invest.viewTransaction(${id});">Transaction</a>'
            },{
            	title:"",
                field:"",         
                width: 80,
                template: '<a href="" onclick="return Invest.setFinished(${id});">Finished</a>'
            },{
            	title:"",
                field:"",         
                width: 65,
                template: '<a href="" onclick="return Invest.delete(${id});">Delete</a>'
            },{
            	title:"",
                field:"",                
            }],
            toolbar: ["Reset"],
            editable:false,
            selectable: 'row',
            dataBound: function(e) {                
                
            }
        });	
        },
        initEvents : function(id)
        {
             $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Invest.create = function()
{
    $.ajax({
        type: 'post',
        url: '/invest/create',
        data: {
            domain : $("#domain").val(),
            deposit : $("#deposit").val(),
            profitDaily : $("#profitDaily").val(),
            planWithdraw : $("#planWithdraw").val(),
            duration : $("#duration").val(),
            investedDate : $("#investedDate").val()
        },
        dataType: 'json',
        success: function(obj){
            //$("#grid").data("kendoGrid").dataSource.read();
            document.location = "/invest/index";
        },
    });
}

Invest.delete = function(id)
{
    if(confirm("Do you want to delete?"))
    {
        $.ajax({
            type: 'post',
            url: '/invest/delete',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Invest.setFinished = function(id)
{
    if(confirm("Do you want to set finished ?"))
    {
        $.ajax({
            type: 'post',
            url: '/invest/setFinished',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Invest.viewTransaction = function(id)
{
     $("#investId").val(id);
     tabs.data("kendoTabStrip").select(2);
     $("#gridTransaction").data("kendoGrid").dataSource.read();
     
     return false;
}